package com.zhao.reggie3.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.reggie3.common.R;
import com.zhao.reggie3.damain.Category;
import com.zhao.reggie3.damain.Setmeal;
import com.zhao.reggie3.dto.SetmealDto;
import com.zhao.reggie3.service.CategoryService;
import com.zhao.reggie3.service.SetmealDishService;
import com.zhao.reggie3.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: zhao
 * @time: 2022/8/20 9:53
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache名称下,所有的缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    @Cacheable(value = "setmealCache",key = "'sermeal_'+#page+'sermeal_'+#pageSize+'sermeal_'+#name")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> page1 = new Page<>();
        Page<SetmealDto> page2 = new Page<>();

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        lqw.like(name != null, Setmeal::getName, name);
        //添加排序条件，根据更新时间降序排列
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(page1, lqw);

        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> collect = records.stream().map((record) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            //分类id
            Long categoryId = record.getCategoryId();
            //根据分类id查询分类对象
            Category categorybyId = categoryService.getById(categoryId);
            //获取分类对象的名字
            if (categorybyId != null) {
                //分类名字
                String name1 = categorybyId.getName();
                //设置分类名字
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        page2.setRecords(collect);
        return R.success(page2);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache名称下,所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //构造查询条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,1);
        //套餐集合
        List<Setmeal> list = setmealService.list(lqw);

        return R.success(list);
    }
}